<?php

namespace app\common\model;

use think\Model;
use traits\model\SoftDelete;

class Student extends Model
{
    //软删除;
    use SoftDelete;

    //教师学生添加
    public function student_add($data)
    {
        $validate = new \app\common\validate\Student();
        if (!$validate->check($data)){
            return $validate -> getError();
        }
        $info = $data['photo'] -> move(ROOT_PATH.'public'.DS.'uploads/student');
        if ($info){
            $data['photo'] = '/uploads/student/'.$info -> getSaveName();
        } else {
            return $data -> getError();
        }
        $result = $this->save($data);
        if ($result){
            return 1;
        } else {
            return '添加失败！';
        }
    }

    //教师学生编辑
    public function student_edit($data)
    {
        $validate = new \app\common\validate\Student();
        if ($data['photos']){
            $data['photo'] = $data['photos'];
            if (!$validate->check($data)){
                return $validate -> getError();
            }
            $info = $data['photo'] -> move(ROOT_PATH.'public'.DS.'uploads/student');
            if ($info){
                $data['photo'] = '/uploads/student/'.$info -> getSaveName();
            } else {
                return $data -> getError();
            }
        }
        $student = $this -> find($data['id']);
        $student -> sname = $data['sname'];
        $student -> age = $data['age'];
        $student -> sex = $data['sex'];
        $student -> address = $data['address'];
        $student -> score = $data['score'];
        $student -> motto = $data['motto'];
        $student -> school = $data['school'];
        $student -> photo = $data['photo'];
        $result = $student -> save();
        if ($result){
            return 1;
        } else {
            return '更新失败！';
        }
    }
}
